﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
using System.Drawing;

namespace MusicAcademy
{
    class Globals
    {
        public static bool IsDigitOrCommand(char c)
        {
            if (char.IsDigit(c))
                return true;
            
            if ((int)c == 8)//Backspace
                return true;

            return false;
        }

        public static string Get10CharDate()
        {
            return Get10CharDate(DateTime.Now);
        }

        public static string Get10CharDate(DateTime d)
        {
            PersianCalendar p = new PersianCalendar();
            return p.GetYear(d).ToString("0000") + "/" + p.GetMonth(d).ToString("00") + "/" + p.GetDayOfMonth(d).ToString("00");
        }

        public static DateTime GregorianDate(string PersianDate)
        {
            PersianCalendar p = new PersianCalendar();
            string[] s = PersianDate.Split('/');
            return p.ToDateTime(int.Parse(s[0]), int.Parse(s[1]), int.Parse(s[2]), 0, 0, 0, 0, System.Globalization.GregorianCalendar.ADEra);
        }

        public static int GetDOW(DayOfWeek dow)
        {
            int d = ((int)dow + 2) % 7;
            if (d == 0)
                d = 7;
            return d;
        }

        public static bool SaveTransaction(int personID, string date, long credit, long debit, string description)
        {
            try
            {
                SqlConnection con = new SqlConnection(Properties.Settings.Default.MusicAcademyConnectionString);
                SqlCommand com = new SqlCommand();

                com.Connection = con;
                com.CommandType = CommandType.Text;
                com.CommandText = "INSERT INTO [Transaction] (personID, date, debit, credit, description, academyID, creator, createDate) VALUES (@personID, @date, @debit, @credit, @description, @academyID, @creator, @createDate)";
                if (personID > 0)
                    com.Parameters.AddWithValue("@personID", personID);
                else
                    com.Parameters.AddWithValue("@personID", DBNull.Value);
                com.Parameters.AddWithValue("@date", date);
                com.Parameters.AddWithValue("@debit", debit);
                com.Parameters.AddWithValue("@credit", credit);
                com.Parameters.AddWithValue("@description", description);
                com.Parameters.AddWithValue("@academyID", Program.academyID);
                com.Parameters.AddWithValue("@creator", Program.userID);
                com.Parameters.AddWithValue("@createDate", Globals.Get10CharDate());

                con.Open();
                com.ExecuteNonQuery();
                con.Close();

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool IsInteger(string s)
        {
            long n;
            if (long.TryParse(s, out n) == false)
                return false;
            else
                return true;
        }

        public static bool IsNumeric(string s)
        {
            double n;
            if (double.TryParse(s, out n) == false)
                return false;
            else
                return true;
        }

        public static bool SetError(Control c)
        {
            if (c.GetType() == typeof(DateControl))
            {
                DateControl tmpD = (DateControl)c;
                tmpD.SetBackColor(Color.LightSalmon);
                if (tmpD.Required)
                    c.Tag = Color.FromArgb(192, 192, 255);
                else
                    c.Tag = Color.White;
            }
            else
            {
                if (c.Tag == null)
                    c.Tag = c.BackColor;
                c.BackColor = Color.LightSalmon;
            }
            return false;
        }

        public static void SetValid(Control c)
        {
            if (c.GetType() == typeof(DateControl))
            {
                DateControl tmpD = (DateControl)c;
                if (tmpD.Required)
                    tmpD.SetBackColor(Color.FromArgb(192, 192, 255));
                else
                    tmpD.SetBackColor(Color.White);
            }
            else
            {
                if (c.Tag != null)
                    c.BackColor = (Color)c.Tag;
            }
        }

        public static bool IsValidTime(string t)
        {
            int a;
            
            if (t.Trim().Length < 5)
                return false;

            a = int.Parse(t.Substring(0, 2));
            if (a < 0 || a > 23)
                return false;

            a = int.Parse(t.Substring(3, 2));
            if (a < 0 || a > 59)
                return false;

            return true;
        }
    }
}
